<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-12
 * Time: 14:18
 */

namespace App\Http\Controllers\Admin;

use App\Base\BaseFunc;
use App\Http\Controllers\Controller;
use App\Model\Shop;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class AdminController extends Controller
{
    public function adminLogin()
    {
        return view("admin.adminLogin");
    }

    public function _adminLogin(BaseFunc $baseFunc)
    {
        $input = Input::only('shop_phone','shop_password');
        $password = md5($input['shop_password']);
        $check=Shop::where('shop_phone',$input['shop_phone'])->first();
        if ($check)
        {
            if ($check->shop_password == $password)
            {
                session(['shop_id' => $check->shop_id]);
                session(['shop_name' => $check->shop_name]);
                $baseFunc->setRedirectMessage(true,"登录成功！",NULL,'/addService');
            }
            else
            {
                $baseFunc->setRedirectMessage(false,'密码错误',NULL,'/adminLogin');
            }
        }
        else
        {
            $baseFunc->setRedirectMessage(false,'您不是商家',NULL,'/adminLogin');
        }
    }

    public function adminRegister()
    {
        return view('/admin.adminRegister');
    }

    public function _adminRegister(Request $request,BaseFunc $baseFunc)
    {
        $file = $request->file('shop_img');
        $file_name = date('YmdHis').rand(1000,9999).".".$file->getClientOriginalExtension();
        $destinationPath = public_path() . '/images';
        $file->move($destinationPath, $file_name);

        $input = Input::only('shop_name','shop_password');
        $password = md5($input['shop_password']);
        $check = Shop::where('shop_name',$input['shop_name'])->first();
        if ($check)
        {
            $baseFunc->setRedirectMessage(false,'重复的店铺名',NULL,'/adminRegister');
        }
        else
        {
            Shop::create([
                'shop_name' => $request->input('shop_name'),
                'shop_password' => $password,
                'shop_phone' => $request->input('shop_phone'),
                'shop_city' => $request->input('shop_city'),
                'shop_address' => $request->input('shop_address'),
                'shop_img' => $file_name
            ]);
            $baseFunc->setRedirectMessage(true,'注册成功！',NULL,'/adminLogin');
        }
    }

    public function adminLogout(BaseFunc $baseFunc)
    {
        session(["admin_id"=>NULL]);
        $baseFunc ->setRedirectMessage(true,'您已退出登录',NULL,'/adminLogin');
    }

    public function searchAdmin(LeftNavController $leftNavController)
    {
        $data = $leftNavController->liftNav();
        $data['adminData'] = Admin::where('admin_id','!=',session('admin_id'))
            ->where('admin_key','!=',1)
            ->paginate(10);
        session(['now_address' =>'/searchAdmin']);
        session(['now_nav' =>'/base']);
        return view('/admin.base.searchAdmin',$data);
    }

    public function addAdmin(BaseFunc $baseFunc)
    {
        $input = Input::only('admin_name','admin_password');
        $password = md5($input['admin_password']);
        $check = Admin::where('admin_name',$input['admin_name'])->first();
        if ($check)
        {
            $baseFunc->setRedirectMessage(false,'重复的用户名',NULL,'/searchAdmin');
        }
        else
        {
            Admin::create([
                'admin_name' => $input['admin_name'],
                'admin_password' => $password,
                'admin_key' => 2
            ]);
            $baseFunc->setRedirectMessage(true,'添加成功！',NULL,'/searchAdmin');
        }
    }

    public function upAdmin(BaseFunc $baseFunc)
    {
        $input = Input::only('admin_id','admin_password');
        $password = md5($input['admin_password']);
        Admin::where('admin_id',$input['admin_id'])
            ->update([
                'admin_password' => $password
            ]);
        $baseFunc->setRedirectMessage(true,'修改成功',NULL,'/searchAdmin');
    }

    public function delAdmin($id,BaseFunc $baseFunc)
    {
        Admin::destroy($id);
        $baseFunc->setRedirectMessage(true,'删除成功',NULL,'/searchAdmin');
    }
}